package com.yuda.tools.miscellaneous.controller;

import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.tools.miscellaneous.dto.City;
import com.yuda.tools.miscellaneous.dto.School;
import com.yuda.tools.miscellaneous.service.SpartaThreeTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 控制 spartaSchool
 * spartaCity
 * spartaProvince 的状态
 */
@RestController
public class SpartaThreeTable extends BaseController {

    @Autowired
    private SpartaThreeTableService spartaThreeTableService;


    @PostMapping("/updateThreeTable")
    public AjaxResult updateThreeTable(Long provinceId, Long cityId, Long schoolId, Long status) {

        if (status == null) {
            return AjaxResult.error("status 字段必须传");
        }

        if (status == 0) {
            spartaThreeTableService.updateSchool(schoolId, 0);
            //查询这个学校所在的城市里有没有其他的自缴费学校
            int res = spartaThreeTableService.count(String.valueOf(cityId));
            if(res == 0) {
                spartaThreeTableService.updateCity(cityId, 0);
            }

        } else {
            spartaThreeTableService.updateSchool(schoolId, 1);
            spartaThreeTableService.updateProvince(provinceId, 1);
            spartaThreeTableService.updateCity(cityId, 1);
        }
        return AjaxResult.success("成功");
    }

    @GetMapping("/listCity")
    public AjaxResult listCity (Long provinceId) {
        List<City> list = spartaThreeTableService.listCity(provinceId);
        return AjaxResult.success(list);
    }
    @GetMapping("/listSchool")
    public AjaxResult listSchool (Long cityId) {
        List<School> list = spartaThreeTableService.listSchool(cityId);
        return AjaxResult.success(list);
    }

}
